<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('模型名称')" prop="name">
            <el-input v-model="form.name" :placeholder="$t('模型名称')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('模型标识')" prop="key">
            <el-input v-model="form.key" :placeholder="$t('英文,不可重复')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('模型分类')" prop="category">
            <el-select v-model="form.category" filterable :placeholder="$t('请选择')">
              <el-option
                v-for="dict in categoryOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <SaveBtn has-permi="act:process" />
    </div>
  </div>

</template>

<script>
  // 引入和后台交互的JS
  import { saveModel } from '@/api/act/model/actModel'
  export default {
    // eslint-disable-next-line vue/require-prop-types
    props: ['actModelData'],
    data() {
      return {
        // 模型类型字典
        categoryOptions: [],
        form: {},
        // 遮罩层
        loading: false,
        // 表单校验
        rules: {
          name: [
            { required: true, message: this.$t('必填信息'), trigger: 'blur' }
          ],
          key: [
            { required: true, message: this.$t('必填信息'), trigger: 'blur' }
          ]
        }
      }
    },
    watch: {
      actModelData: function() {
        this.form = this.actModelData
      }
    },
    created() {
      // 模型类型字典
      this.getDicts('act_model_type').then(response => {
        this.categoryOptions = response.data
      })
    },
    methods: {
      // 取消按钮
      cancel() {
        this.$parent.$parent.open = false
        this.$parent.$parent.reset()
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            this.loading = true
            saveModel(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess(this.$t('保存成功'))
                this.$parent.$parent.open = false
                this.$parent.$parent.getList()
                this.loading = false
              } else {
                this.msgError(response.msg)
                this.loading = false
              }
            }).catch(() => {
              this.loading = false
            })
          } else {
            this.locationError(document)
          }
        })
      }

    }

  }

</script>
